
package com.jc.platform.yanglingzhi.controller;


import com.jc.platform.common.result.PageInfo;
import com.jc.platform.yanglingzhi.service.ITTestService;
import com.jc.platform.yanglingzhi.vo.TTestVO;
import com.jc.platform.common.model.QueryParam;
import com.jc.platform.common.model.UserInfo;
import com.jc.platform.common.search.QueryConstant;
import com.jc.platform.common.result.ResultCodeEnum;
import com.jc.platform.common.result.ResultModel;
import com.jc.platform.common.utils.RequestUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.*;

/**
 * ClassName TTestController.java
 * Description 测试表
 *
 * @author 集团管理员
 * @date 2022/01/22
 */
@Slf4j
@RestController
@Api(value = "/tTest", tags = "")
@RequestMapping("tTest")
public class TTestController
{

    private final ITTestService tTestService;

    public TTestController(ITTestService tTestService)
    {
        this.tTestService = tTestService;
    }

	/**
	 * 保存
	 *
	 * @param tTestVO 实体
	 * @return 对象vo
	 */
	@ApiOperation(value = "保存", notes = "保存" )
	@ApiImplicitParam(name = "tTest", value = "实体", required = true, dataType = "TTestVO")
	@PostMapping(value = "/insert", produces = { "application/json;charset=UTF-8" })
	public ResultModel<TTestVO> insert(@RequestBody TTestVO tTestVO)
	{
		try
		{
            UserInfo userInfo = RequestUtil.getLoginUser();
            //赋值集团ID
            tTestVO.setGroupId(userInfo.getGroupId());
            //赋值机构ID
            tTestVO.setOrgId(userInfo.getOrgId());
            //赋值租户ID
            tTestVO.setTenantId(userInfo.getTenantId());
            //赋值部门ID
            tTestVO.setDeptId(userInfo.getDeptId());
            
            
			tTestVO.setCreateTime(LocalDateTime.now());
			tTestVO.setCreateName(userInfo.getUserName());
			tTestVO.setCreateId(userInfo.getUserId());
			tTestVO.setModifyTime(LocalDateTime.now());
			tTestVO.setModifyName(userInfo.getUserName());
			tTestVO.setModifyId(userInfo.getUserId());
            

			return tTestService.insert(tTestVO);
		}
		catch (Exception exception)
		{
			log.error("保存出错:", exception);
			throw exception;
		}
	}
    
	/**
     * 修改
     *
     * @param tTestVO 实体
     * @return 对象vo
     */
    @ApiOperation(value = "修改", notes = "修改" )
    @ApiImplicitParam(name = "tTest", value = "实体", required = true, dataType = "TTestVO")
    @PostMapping(value = "/update", produces = { "application/json;charset=UTF-8" })
    public ResultModel<TTestVO> update(@RequestBody TTestVO tTestVO)
    {
        try
        {
            UserInfo userInfo = RequestUtil.getLoginUser();
			tTestVO.setModifyTime(LocalDateTime.now());
			tTestVO.setModifyName(userInfo.getUserName());
			tTestVO.setModifyId(userInfo.getUserId());

            return tTestService.update(tTestVO);
        }
        catch (Exception exception)
        {
            log.error("修改出错:", exception);
            throw exception;
        }
    }

    /**
     * 删除
     *
     * @param id 主键
     * @return 是否成功
     */
    @ApiOperation(value = "删除", notes = "删除" )
    @ApiImplicitParam(name = "id", value = "实体ID", required = true, dataType = "Long")
    @PostMapping("/delete")
    public ResultModel<Boolean> delete(@RequestParam Long id)
    {
        try
        {
            return tTestService.delete(id);
        }
        catch (Exception exception)
        {
            log.error("删除出错:", exception);
            throw exception;
        }
    }

    /**
     * 批量删除
     *
     * @param ids 主键集合
     * @return 是否成功
     */
    @ApiOperation(value = "批量删除", notes = "批量删除" )
    @ApiImplicitParam(name = "ids", value = "实体ID集合", required = true, dataType = "List<Long>")
    @PostMapping("/deleteBatch")
    public ResultModel<Boolean> deleteBatch(@RequestBody List<Long> ids)
    {
        try
        {
            return tTestService.deleteBatch(ids);
        }
        catch (Exception exception)
        {
            log.error("批量删除出错:", exception);
            throw exception;
        }
    }


    /**
     * 校验属性是否存在，存在返回false，不存在返回true
     *
     * @param id       主键
     * @param property 属性名称
     * @param value    属性值
     * @return 检查结果
     */
    @ApiOperation(value = "校验属性是否存在", notes = "校验属性是否存在，存在返回false，不存在返回true" )
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "实体ID", dataType = "Long"), @ApiImplicitParam(name = "property", value = "属性名称", required = true, dataType = "String"),
    @ApiImplicitParam(name = "value", value = "属性值", required = true, dataType = "String") })
    @GetMapping("/checkNotExists")
    public ResultModel<Boolean> checkNotExists(@RequestParam(required = false) Long id, @RequestParam String property, @RequestParam String value)
    {
        try
        {
            return ResultModel.success(tTestService.checkNotExists(id, property, value));
        }
        catch (Exception exception)
        {
            log.error("校验属性是否存在出错:", exception);
            throw exception;
        }
    }

    /**
     * 查询对象
     *
     * @param id 主键
     * @return 对象
     */
    @ApiOperation(value = "查询对象", notes = "查询对象" )
    @ApiImplicitParam(name = "id", value = "实体ID", required = true, dataType = "Long")
    @GetMapping("/get")
    public ResultModel<TTestVO> get(@RequestParam Long id)
    {
        try
        {
            return tTestService.get(id);
        }
        catch (Exception exception)
        {
            log.error("查询对象出错:", exception);
            throw exception;
        }
    }

	/**
	 * 查询对象集合
	 *
	 * @param sortBy      排序字段
	 * @param sortOrder   排序方式
	 * @param queryString 查询条件,查询条件按规则拼接后的字符串
	 * @return 对象集合
	 */
	@ApiOperation(value = "查询对象集合", notes = "查询对象集合")
	@ApiImplicitParams({ @ApiImplicitParam(name = "sortBy", value = "排序字段,和属性名称一致", dataType = "String"),
			@ApiImplicitParam(name = "sortOrder", value = "排序方式", allowableValues = "ASC,DESC", dataType = "String"),
			@ApiImplicitParam(name = "q", value = "查询条件,查询条件按规则拼接后的字符串", required = true, dataType = "String") })
	@GetMapping("/find")
    public ResultModel<List<TTestVO>> find(@RequestParam(required = false) String sortBy, @RequestParam(required = false) String sortOrder, @RequestParam(QueryConstant.Q_PARAM) String queryString)
    {
        try
        {
            QueryParam queryParam = new QueryParam();
            queryParam.setSortOrder(sortOrder);
            queryParam.setSortBy(sortBy);
            queryParam.setQ(queryString);
            return tTestService.find(queryParam);
        }
        catch (Exception exception)
        {
            log.error("查询对象集合出错:", exception);
            throw exception;
        }
    }

	/**
	 * 分页查询
	 *
	 * @param pageNum     查询页码
	 * @param pageSize    每页记录数
	 * @param sortBy      排序字段
	 * @param sortOrder   排序方式
	 * @param queryString 查询条件,查询条件按规则拼接后的字符串
	 * @return 分页对象
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@ApiImplicitParams({ @ApiImplicitParam(name = "pageNum", value = "查询页码", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "pageSize", value = "每页记录数", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "sortBy", value = "排序字段,和属性名称一致", required = true, dataType = "String"),
			@ApiImplicitParam(name = "sortOrder", value = "排序方式", allowableValues = "ASC,DESC", required = true, dataType = "String"),
			@ApiImplicitParam(name = "q", value = "查询条件,查询条件按规则拼接后的字符串", required = true, dataType = "String") })
	@GetMapping("/finds")
    public ResultModel<PageInfo<TTestVO>> finds(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam String sortBy, @RequestParam String sortOrder,
           @RequestParam(QueryConstant.Q_PARAM) String queryString)
    {
        try
        {
            QueryParam queryParam = new QueryParam();
            queryParam.setPageNum(pageNum);
            queryParam.setPageSize(pageSize);
            queryParam.setSortOrder(sortOrder);
            queryParam.setSortBy(sortBy);
            queryParam.setQ(queryString);
            return tTestService.finds(queryParam);
        }
        catch (Exception exception)
        {
            log.error("分页查询出错:", exception);
            throw exception;
        }
    }







}

